const {app,BrowserWindow,ipcMain, Tray,Menu,ipcRenderer} = require('electron')
const path = require('path');
const child_process = require("child_process")    //使用bat
// child_process.execFile
let newWin = null           //新页面
let newWinTimeIsUp = null   //事件到了
let newWinError = null      //输入错误
let todolistPage = null     //todolist页
let sleepPage = null        //休息页
let timeWait = null         //待开发页
let getTianQi = null        //天气页
let learnWin = null         //学习页
//学习页分支
let learnGoWin = null
let learnVueWin = null
let learnReactWin = null
let learnPythonWin = null

let getTimeHour = 0
let getTimeMinte = 0
let getSleep_hour = 0
let getSleep_minte = 0

let tray = null  // 在外面创建tray变量，防止被自动删除，导致图标自动消失

function createWindow(){
    const mainWin = new BrowserWindow({
        width:800,
        height:600,
        webPreferences:{
            nodeIntegration: true,
            enableRemoteModule: true,
            contextIsolation: false,
        },
        resizable: false, //禁止改变主窗口尺寸
        frame:false,
        transparent:true,
    })
    mainWin.loadFile("./index.html")
    // mainWin.setMenu(null)
    //mainWin ipcMain begin
    ipcMain.on('getHour',async(event,value)=>{
        console.log(value)
        getTimeHour = await value;
        event.reply('returnHour','hour')
    })//获取时间 - 时
    ipcMain.on('sleepTime_H',async(event,value)=>{
        console.log(value)
        getSleep_hour = await value;
        // event.reply('returnSleepTime_H',value)
    })//获取休息时间 - 时
    ipcMain.on('sleepTime_M',async(event,value)=>{
        console.log(value)
        getSleep_minte = await value;
        // event.reply('returnSleepTime_M',value)
    })//获取休息时间 - 分
    ipcMain.on('getMinute',async(event,value)=>{
        console.log(value)
        getTimeMinte = await value;
        event.reply('returnMinte','minute')
    })//获取时间 - 分
    //获取天气页
    ipcMain.on('getTianQi',()=>{
        if(getTianQi == null){
        getTianQi = new BrowserWindow({
            width:400,
            height: 200,
            webPreferences:{
                nodeIntegration: true,
                enableRemoteModule: true,
                contextIsolation: false,
            },
            resizable: false, //禁止改变主窗口尺寸
            frame:false,
            transparent:true,
        })
        getTianQi.loadFile('./getTianQi.html')
        getTianQi.hide()
        ipcMain.on('tianqi-top',()=>{
            getTianQi.setAlwaysOnTop(true)
        })
        ipcMain.on('tianqi-no-top',()=>{
            getTianQi.setAlwaysOnTop(false)
        })
        ipcMain.on('tianqi-close',()=>{
            getTianQi.close();
            if(newWin != null){
                newWin.close();
            }
            if(learnWin != null){
                learnWin.close();
            }
            if(learnGoWin != null){
                learnGoWin.close();
            }
            if(learnVueWin != null){
                learnVueWin.close()
            }
            if(learnReactWin != null){
                learnReactWin.close()
            }
            if(learnPythonWin != null){
                learnPythonWin.close()
            }
        })
        ipcMain.on('show-tianqi',()=>{
            getTianQi.show()
            newWin.hide()
        })
        ipcMain.on('show-newWin-right',()=>{
            newWin.show()
            getTianQi.hide()
        })
        }
    })
    //获取学习页
    ipcMain.on('getLearnWin',()=>{
        if(learnWin == null){
            learnWin = new BrowserWindow({
                width:400,
                height: 200,
                webPreferences:{
                    nodeIntegration: true,
                    enableRemoteModule: true,
                    contextIsolation: false,
                },
                resizable: false, //禁止改变主窗口尺寸
                frame:false,
                transparent:true,
            })
            learnWin.loadFile('./learnPage.html')
            learnWin.hide()
            ipcMain.on('learn-top',()=>{
                learnWin.setAlwaysOnTop(true)
            })
            ipcMain.on('learn-no-top',()=>{
                learnWin.setAlwaysOnTop(false)
            })
            ipcMain.on('learn-close',()=>{
                learnWin.close();
                if(newWin != null){
                    newWin.close();
                }
                if(getTianQi != null){
                    getTianQi.close();
                }
                if(learnGoWin != null){
                    learnGoWin.close();
                }
                if(learnVueWin != null){
                    learnVueWin.close()
                }
                if(learnReactWin != null){
                    learnReactWin.close()
                }
                if(learnPythonWin != null){
                    learnPythonWin.close()
                }
            })
            ipcMain.on('show-learn',()=>{
                learnWin.show()
                newWin.hide()
            })
            ipcMain.on('show-newWin-left',()=>{
                newWin.show()
                learnWin.hide()
            })
            //go page
            ipcMain.on('learn-go-page',()=>{
                if(learnGoWin == null){
                    learnGoWin = new BrowserWindow({
                        width:400,
                        height: 200,
                        webPreferences:{
                            nodeIntegration: true,
                            enableRemoteModule: true,
                            contextIsolation: false,
                        },
                        resizable: false, //禁止改变主窗口尺寸
                        frame:false,
                        transparent:true,
                    })
                    learnGoWin.loadFile('./learn/golang.html')
                    learnGoWin.hide()
                    ipcMain.on('show-learn-go',()=>{
                        learnWin.hide()
                        learnGoWin.show()
                    })
                    ipcMain.on('learn-return',()=>{
                        learnWin.show()
                        learnGoWin.hide()
                    })
                }
            })
            //vue page
            ipcMain.on('learn-vue-page',()=>{
                if(learnVueWin == null){
                    learnVueWin = new BrowserWindow({
                        width:400,
                        height: 200,
                        webPreferences:{
                            nodeIntegration: true,
                            enableRemoteModule: true,
                            contextIsolation: false,
                        },
                        resizable: false, //禁止改变主窗口尺寸
                        frame:false,
                        transparent:true,
                    })
                    learnVueWin.loadFile('./learn/vue.html')
                    learnVueWin.hide()
                    ipcMain.on('show-learn-vue',()=>{
                        learnWin.hide()
                        learnVueWin.show()
                    })
                    ipcMain.on('learn-return',()=>{
                        learnWin.show()
                        learnVueWin.hide()
                    })
                }
            })
            //react page
            ipcMain.on('learn-react-page',()=>{
                if(learnReactWin == null){
                    learnReactWin = new BrowserWindow({
                        width:400,
                        height: 200,
                        webPreferences:{
                            nodeIntegration: true,
                            enableRemoteModule: true,
                            contextIsolation: false,
                        },
                        resizable: false, //禁止改变主窗口尺寸
                        frame:false,
                        transparent:true,
                    })
                    learnReactWin.loadFile('./learn/react.html')
                    learnReactWin.hide()
                    ipcMain.on('show-learn-react',()=>{
                        learnWin.hide()
                        learnReactWin.show()
                    })
                    ipcMain.on('learn-return',()=>{
                        learnWin.show()
                        learnReactWin.hide()
                    })
                }
            })
            //python page
            ipcMain.on('learn-python-page',()=>{
                if(learnPythonWin == null){
                    learnPythonWin = new BrowserWindow({
                        width:400,
                        height: 200,
                        webPreferences:{
                            nodeIntegration: true,
                            enableRemoteModule: true,
                            contextIsolation: false,
                        },
                        resizable: false, //禁止改变主窗口尺寸
                        frame:false,
                        transparent:true,
                    })
                    learnPythonWin.loadFile('./learn/python.html')
                    learnPythonWin.hide()
                    ipcMain.on('show-learn-python',()=>{
                        learnWin.hide()
                        learnPythonWin.show()
                    })
                    ipcMain.on('learn-return',()=>{
                        learnWin.show()
                        learnPythonWin.hide()
                    })

                }
            })
        }
    })
    //获取新页面
    ipcMain.on('mainwin-new-win',()=>{
        newWin = new BrowserWindow({
            width:400,
            height: 200,
            webPreferences:{
                nodeIntegration: true,
                enableRemoteModule: true,
                contextIsolation: false,
            },
            resizable: false, //禁止改变主窗口尺寸
            frame:false,
            transparent:true,
        })
        newWin.loadFile("./onePage.html")
        // newWin.setAlwaysOnTop(true)
        //newWin置顶
        ipcMain.on('newWin-top',()=>{
            newWin.setAlwaysOnTop(true)
        })
        ipcMain.on('newWin-no-top',()=>{
            newWin.setAlwaysOnTop(false)
        })
        //newWin ipcMain begin
        ipcMain.on('new-win-hour',(event,value)=>{
            // console.log(value)
            event.reply('new-win-returnHour',getTimeHour)
        })
        ipcMain.on('new-win-minue',(event,value)=>{
            // console.log(value)
            event.reply('new-win-returnMinue',getTimeMinte)
        })
        ipcMain.on('new-win-sleeptime-h',(event,value)=>{
            event.reply('new-win-sleep-returnHour',getSleep_hour)
        })
        ipcMain.on('new-win-sleeptime-m',(event,value)=>{
            event.reply('new-win-sleep-returnMinue',getSleep_minte)
        })
        //sleep-page
        ipcMain.on('go-SleepTime',(event,value)=>{
            console.log('go-SleepTime:',value)
            if(sleepPage == null){
                sleepPage = new BrowserWindow({
                    width:400,
                    height: 200,
                    webPreferences:{
                        nodeIntegration: true,
                        enableRemoteModule: true,
                        contextIsolation: false,
                    },
                    resizable: false, //禁止改变主窗口尺寸
                    frame:false,
                    transparent:true,
                })
                sleepPage.loadFile("./sleepTime.html")
                newWin.close()
                getTianQi.close()
                ipcMain.on('sleeptime-top',()=>{
                    sleepPage.setAlwaysOnTop(true)
                })
                ipcMain.on('sleeptime-no-top',()=>{
                    sleepPage.setAlwaysOnTop(false)
                })
                ipcMain.on('sleeptime-close',()=>{
                    sleepPage.close();
                })

            }
        })
        //获取天气-页
        ipcMain.on('getTianQi',()=>{
            if(getTianQi == null){
            }
        })

        //time-wait-page
        ipcMain.on('time-wait',()=>{
            if(timeWait == null){
                timeWait = new BrowserWindow({
                    width:400,
                    height: 200,
                    webPreferences:{
                        nodeIntegration: true,
                        enableRemoteModule: true,
                        contextIsolation: false,
                    },
                    resizable: false, //禁止改变主窗口尺寸
                    frame:false,
                    transparent:true,
                })
                timeWait.loadFile('./timewait.html')
                ipcMain.on('time-wait-top',()=>{
                    timeWait.setAlwaysOnTop(true)
                })
                ipcMain.on('time-wait-no-top',()=>{
                    timeWait.setAlwaysOnTop(false)
                })
                ipcMain.on('time-wait-close',()=>{
                    timeWait.close()
                })
                newWin.close()
            }
        })
        //tolist-page
        ipcMain.on('go-todolistPage',()=>{
            //todolist
            if(todolistPage == null){
                todolistPage = new BrowserWindow({
                    width: 400,
                    height: 200,
                    webPreferences:{
                        nodeIntegration: true,
                        enableRemoteModule: true,
                        contextIsolation: false,
                    },
                    resizable: false,
                    frame: false,
                    transparent:true,
                })
                todolistPage.loadFile("./todoList.html")
            }
            /*todolist ipcMain begin */
            ipcMain.on('todo-top',()=>{
                todolistPage.setAlwaysOnTop(true)
            })
            ipcMain.on('todo-no-top',()=>{
                todolistPage.setAlwaysOnTop(false)
            })
            ipcMain.on('todo-close',()=>{
                todolistPage.close();
            })
            /*todolist ipcMain end */
            todolistPage.on('closed',()=>{
                todolistPage = null;
            })
        })
        ipcMain.on('time-isUp-new-win',()=>{
            //time is up
            newWinTimeIsUp = new BrowserWindow({
                width:400,
                height: 200,
                webPreferences:{
                    nodeIntegration: true,
                    enableRemoteModule: true,
                    contextIsolation: false,
                },
                resizable: false, //禁止改变主窗口尺寸
                frame: false,
                transparent:true,
            })
            newWin.close()
            learnWin.close()
            if(learnGoWin != null){
                learnGoWin.close()
            }
            if(learnVueWin != null){
                learnVueWin.close()
            }
            if(learnReactWin != null){
                learnReactWin.close()
            }
            if(learnPythonWin != null){
                learnPythonWin.close()
            }
            getTianQi.close()

            /*time is up ipcMain begin*/
            ipcMain.on('newWin-isup-top',()=>{
                newWinTimeIsUp.setAlwaysOnTop(true)
            })
            ipcMain.on('newWin-no-isup-top',()=>{
                newWinTimeIsUp.setAlwaysOnTop(false)
            })
            ipcMain.on('new-win-isup-close',()=>{
                newWinTimeIsUp.close();
            })
            /*time is up ipcMain end */
            newWinTimeIsUp.loadFile('./timeIsUp.html')
            newWinTimeIsUp.setMenu(null)
            newWin.close()
            todolistPage.close()
            newWinTimeIsUp.on('closed',()=>{
                newWinTimeIsUp = null;
            })
            
        })
        ipcMain.on('time-error-new-win',()=>{
            newWinError = new BrowserWindow({
                width:400,
                height:200,
                webPreferences:{
                    nodeIntegration: true,
                    enableRemoteModule: true,
                    contextIsolation: false,
                },
                resizable: false, //禁止改变主窗口尺寸
                frame: false,
                transparent:true,
            })
            newWinError.loadFile('./timeError.html')
            /*time is error ipcMain begin*/
            ipcMain.on('newWin-error-top',()=>{
                newWinError.setAlwaysOnTop(true)
            })
            ipcMain.on('newWin-no-error-top',()=>{
                newWinError.setAlwaysOnTop(false)
            })
            ipcMain.on('new-win-error-close',()=>{
                newWinError.close();
                if(newWin != null){
                    newWin.close()
                }
                if(getTianQi != null){
                    getTianQi.close()
                }
                if(learnWin != null){
                    learnWin.close()
                }
                if(learnGoWin != null){
                    learnGoWin.close()
                }
                if(learnVueWin != null){
                    learnVueWin.close()
                }
                if(learnReactWin != null){
                    learnReactWin.close()
                }
                if(sleepPage != null){
                    sleepPage.close()
                }
            })
            /*time is error ipcMain end */
            newWinError.setMenu(null)
            newWin.close()

        })
        ipcMain.on('new-win-close',()=>{
            newWin.close();
            if(getTianQi != null){
                getTianQi.close();
            }
            if(learnWin != null){
                learnWin.close();
            }
        })
        //newWin ipcMain end
        // newWin.setMenu(null)
        mainWin.close()
        newWin.on('closed',()=>{
            newWin = null
        })
    })

    ipcMain.on('main-top',()=>{
        mainWin.setAlwaysOnTop(true)
    })
    ipcMain.on('main-no-top',()=>{
        mainWin.setAlwaysOnTop(false)
    })
    /* ipcMain.on('main-close',()=>{
        mainWin.close();
    }) */
    //mainWin ipcMain end

    //后端任务
    ipcMain.on('main-close',(e)=>{
        e.preventDefault();
        mainWin.setSkipTaskbar(true) //取消任务栏显示
        mainWin.hide();//隐藏主窗口
    })

    //创建任务栏图标
    tray = new Tray(path.join(__dirname,'images','favicon.ico'))
     // 自定义托盘图标的内容菜单
  const contextMenu = Menu.buildFromTemplate([
    {
      // 点击退出菜单退出程序
      label: '退出', click: function () {
        console.log(123);
        mainWin.destroy()
        app.quit()

      }
    }
  ])
  tray.setToolTip('demo')  // 设置鼠标指针在托盘图标上悬停时显示的文本
  tray.setContextMenu(contextMenu)  // 设置图标的内容菜单
  // 点击托盘图标，显示主窗口
  tray.on("click", () => {
    mainWin.show();
  })
}

app.whenReady().then(()=>{
    createWindow()
})

app.on('window-all-closed',()=>{
    app.quit()
})

process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';


// 在入口文件对的顶部配置热加载(主进程)
// 热加载
try {
	require('electron-reloader')(module, {});
} catch (_) { }